import xlrd
import xlwt

application_data_path = r'../data/application.xlsx'
machine_data_path = r'../data/服务器.xlsx'
ip_data_path = r'../data/ip.xlsx'
database_data_path = r'../data/数据库.xlsx'
app_component_data_path = r'../data/应用组件.xlsx'


topology_data_path = r'../result/topology.xlsx'



def create_topology():
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('sheet1',cell_overwrite_ok=True)
    worksheet.write(0, 0, label='antecedent')
    worksheet.write(0, 1, label='consequent')

    topology_index = 1;


    #应用组件到IP
    excel_data = xlrd.open_workbook(ip_data_path)
    table = excel_data.sheets()[0]
    rows = table.nrows

    for i in range(4, rows - 1):
        app_comp_name = table.cell(i,23).value
        ip = table.cell(i,2).value
        if app_comp_name:
            worksheet.write(topology_index, 0, label=app_comp_name)
            worksheet.write(topology_index, 1, label=ip)
            topology_index = topology_index + 1


    # 应用到服务器
    excel_data = xlrd.open_workbook(machine_data_path)
    table = excel_data.sheets()[0]
    rows = table.nrows
    for i in range(4, rows - 1):
        app_name = table.cell(i,17).value
        machine_name = table.cell(i,2).value
        if app_name:
            worksheet.write(topology_index, 0, label=app_name)
            worksheet.write(topology_index, 1, label=machine_name)
            topology_index = topology_index + 1



    #应用到数据库
    excel_data = xlrd.open_workbook(database_data_path)
    table = excel_data.sheets()[0]
    rows = table.nrows
    for i in range(4, rows - 1):
        app_name = table.cell(i,17).value
        database_name = table.cell(i,2).value
        if app_name:
            worksheet.write(topology_index, 0, label=app_name)
            worksheet.write(topology_index, 1, label=database_name)
            topology_index = topology_index + 1


    #应用到应用组件
    excel_data = xlrd.open_workbook(app_component_data_path)
    table = excel_data.sheets()[0]
    rows = table.nrows
    for i in range(4, rows - 1):
        app_name = table.cell(i,18).value
        app_component_name = table.cell(i,2).value
        if app_name:
            worksheet.write(topology_index, 0, label=app_name)
            worksheet.write(topology_index, 1, label=app_component_name)
            topology_index = topology_index + 1

    workbook.save(topology_data_path)


create_topology()